

local printx = debug.printx
local LIBDEMO = _G.libdemo

function __init__(self)
    self.m_AllSceneByID = self.m_AllSceneByID or {}
    self.m_AllocSceneUid = self.m_AllocSceneUid or 0
    self.m_AllSceneByUid = self.m_AllSceneByUid or {}
end

function AllocSceneUid(self)
	self.m_AllocSceneUid = self.m_AllocSceneUid + 1
	return self.m_AllocSceneUid
end

function AddScene(self, sceneID, sceneUid)
    sceneUid = sceneUid or self:AllocSceneUid()
    local scene = self:CreateScene(sceneID, sceneUid)
    self.m_AllSceneByUid[sceneUid] = scene
    local list = self.m_AllSceneByID[sceneID]
	if not list then
		self.m_AllSceneByID[sceneID] = {[sceneUid] = scene}
	else
		list[sceneUid] = scene
	end
    return scene
end

function CreateSceneAoi(self, SceneUid)
    local function aoiCallback(scendUid, event, objId, list)
        printx("aoiCallback:",scendUid, event, objId)
        table.print(list)
        local scene = self:GetSceneByUid(scendUid)
        if not scene then return end

        local targetObj = scene:GetObjByUid(objId)
        if not targetObj then return end
        if event == EnAoiEvent.FansEnter then
            --objId 进入 list 玩家的视野
            for _,uid in ipairs(list) do
                local obj = scene:GetObjByUid(uid)
                if obj.OnObjEnter then
                    xpcall(function ()
                        obj:OnObjEnter(targetObj)
                    end)
                end
            end

        elseif event == EnAoiEvent.MarkEnter then
            --list 玩家进入 objId 的视野
            if targetObj.OnObjEnter then
                for _,uid in ipairs(list) do
                    local obj = scene:GetObjByUid(uid)
                    xpcall(function ()
                        targetObj:OnObjEnter(obj)
                    end)
                end
            end

        elseif event == EnAoiEvent.FansLeave then
            --objId 离开 list 玩家的视野
            for _,uid in ipairs(list) do
                local obj = scene:GetObjByUid(uid)
                if obj.OnObjLeave then
                    xpcall(function ()
                        obj:OnObjLeave(targetObj)
                    end)
                end
            end

        elseif event == EnAoiEvent.MarkLeave then
            --list 玩家 离开 objId 的视野
            if targetObj.OnObjLeave then
                for _,uid in ipairs(list) do
                    local obj = scene:GetObjByUid(uid)
                    xpcall(function ()
                        targetObj:OnObjLeave(obj)
                    end)
                end
            end
        end
    end

    return LIBDEMO.aoi_create(SceneUid, true, aoiCallback)
end

function CreateScene(self, sceneID, sceneUid)
    local oci = {
        SceneId = sceneID,
        SceneUid = sceneUid,
        AoiRange = 1,
        AoiMgr = self:CreateSceneAoi(sceneUid)
    }
    local scene = CORE_SCENE.clsCoreScene:New()
	scene:OnCreate(oci)

    return scene
end

function DelScene(self, scene)
    local sceneUid = scene:GetUid()
    scene:OnDestroy()
    local sceneID = scene:GetSceneID()
    self.m_AllSceneByUid[sceneUid] = nil
    local list = self.m_AllSceneByID[sceneID]
	if list then
		list[sceneUid] = nil
		if not next(list) then
			self.m_AllSceneByID[sceneID] = nil
		end
	end
end

function GetSceneByUid(self, sceneUid)
    return self.m_AllSceneByUid[sceneUid]
end


function GotoScene(self, user, sceneID, pos, sceneUid)
    local x, y
	if pos then
		x, y = table.unpack(pos)
	end

	x = x or 0
	y = y or 0

    local scene
	if sceneUid then
		scene = sceneUid and self.m_AllSceneByUid[sceneUid]
	end

    if not scene then
        scene = self:AddScene(sceneID, sceneUid)
    end

    user:EnterScene(pos, scene)
end




